define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'profession/profession/index' + location.search,
                    add_url: 'profession/profession/add',
                    edit_url: 'profession/profession/edit',
                    //del_url: 'profession/del',
                    multi_url: 'profession/profession/multi',
                    import_url: 'profession/profession/import',
                    table: 'profession',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                // 表单
                var form = $("form", table.$commonsearch);

                // 设置绑定地区
                $("input[name='region.id']", form).addClass("selectpage").data("source", "region/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);

                // 注册事件
                Form.events.selectpage(form);
            });


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sort desc,id desc',
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name') },
                        { field: 'exam.name', title: __('Exam') },
                        { field: 'code', title: __('Code') },
                        {
                            field: 'layer',
                            title: __('Layer'),
                            formatter: Table.api.formatter.normal,
                            searchList: { 0: __('-'), 1: __('本科'), 2: __('专科') },
                            visible: false,
                            searchable: false
                        },
                        { field: 'region.name', title: __('Region'), searchable: false },
                        { field: 'region.id', title: __('Region'), visible: false },
                        { field: 'old_name', title: __('Old_name'), visible: false, searchable: false },
                        {
                            field: 'exam_time', 
                            title: __('考试时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.date,
                            visible: false
                        },
                        {
                            field: 'exam_time_text',
                            title: __('考试时间'),
                            operate: 'RANGE',
                            searchable: false,
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.date
                        },
                        {
                            field: 'sort',
                            title: __('排序值'),
                            searchable: false
                        },
                        {
                            field: 'a_time',
                            title: __('A_time'),
                            searchable: false,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'u_time',
                            title: __('U_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.normal,
                            searchList: { 1: __('是'), 2: __('否') }
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '管理此专业的题库',
                                    title: '管理此专业的题库',
                                    icon: 'fa fa-question-circle',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: 'profession/course/index'
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });


            $('.btn-batch-setexamtime').click(function (){
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Layer.alert(__('请选择需要操作的专业'));
                    return false;
                }
                var url = 'profession/profession/batchsetexamtime?ids=' + ids.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('批量设置考试时间'), $(this).data() || {}, {
                    success: function (data, ret) {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            // 考试分类修改后需要修改专业分类的请求参数
            $('#c-exam_id').on('change', function () {
                var value = $("input[name='" + $(this).attr('name').slice(0, -5) + "']").val()
                $("#c-category_id").data("params", function (obj) {
                    return { custom: { exam_id: value } };
                });
                Controller.api.bindevent();
            })
            // 专业分类默认值请求参数
            $("#c-category_id").data("params", function (obj) {
                return { custom: { exam_id: $('#c-exam_id').val() } };
            });
            Controller.api.bindevent();
        },
        batchsetexamtime: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
